﻿--[[
   @brief    NURailBranch(单机游戏多分支选择)
   @author   jeffrywang
   @version  1.0
   @date     2016-12-28
--]]

base_utility.log("loading rail_branch_item.lua");

ierd_tgp.tpf_ui.Require("common\\common.lua");
ierd_tgp.tpf_ui.Require("frame_bottom\\rail_branch_list.lua");

NURailBranch = {
    uilib_ = 0;
    game_id_ = 0;
    ctn_id_ = 0;
    ctrl_id_ = 0;
    state_ = 0;
    enabled_ = true;
    expanded_ = false;
    branch_infos = {};
    
    init = function(game_id)
        base_utility.log("NURailBranch.init,  game_id:".. tostring(game_id));
        
        -- 切换分支按钮容器
        local ctn_id = frame_bottom_view.bottom_branch_info_ctn_id_; 
        -- 按钮分组
        local btns_id = ui_utility.get_ctrl_id_by_path(ctn_id, "Bottom_BranchInfo_Ctn");
        
        base_utility.log("NURailBranch.init, got btns_id:".. tostring(btns_id));
        
        NURailBranch.uilib_ = ui_utility.uilib();
        NURailBranch.game_id_ = game_id;
        NURailBranch.ctn_id_ = ctn_id;
        NURailBranch.ctrl_id_ = btns_id;
        NURailBranch.valid_ = true;
        NURailBranch.state_ = 0;
        NURailBranch.expanded_ = false;
        
        NURailBranch.branch_infos[game_id] = frame_bottom_model.get_all_branch_infos(game_id);
       
        NURailBranch.show(false);
        NURailBranch.set_expanded(false);
    end;

    log = function(msg, core_msg)
        base_utility.log(string.format("[WGBanner][%d][Branch] %s", NURailBranch.game_id_, msg), core_msg);
    end;

    set_visible = function(visible)
        NURailBranch.log(string.format("[NURailBranch][%d]set_visible %s", NURailBranch.game_id_, tostring(visible)));
        MainCommon.set_visible(NURailBranch.uilib_, NURailBranch.ctrl_id_, visible);
    end;

    set_local_valid = function(is_valid)
        NURailBranch.log(string.format("[NURailBranch][%d]set_local_valid %s", NURailBranch.game_id_, tostring(is_valid)));
        if nil == is_valid then
            return;
        end

        NURailBranch.valid_ = is_valid;
        MainCommon.set_enabled(NURailBranch.uilib_,
            NURailBranch.ctrl_id_, "CntBranchButtons", is_valid);
        MainCommon.set_enabled(NURailBranch.uilib_,
            NURailBranch.ctrl_id_, "CntBranchButtons\\BtnBranchDown", is_valid);
        MainCommon.set_enabled(NURailBranch.uilib_,
            NURailBranch.ctrl_id_, "BtnBranchDownCover", is_valid);
        MainCommon.set_enabled(NURailBranch.uilib_,
            NURailBranch.ctrl_id_, "CntBranchButtons\\BtnBranchUp", is_valid);
        MainCommon.set_enabled(NURailBranch.uilib_,
            NURailBranch.ctrl_id_, "BtnBranchName", is_valid);
 
        MainCommon.show_child(NURailBranch.uilib_,
            NURailBranch.ctrl_id_, "CntBranchButtons\\BtnBranchUp", is_valid);
        MainCommon.show_child(NURailBranch.uilib_,
            NURailBranch.ctrl_id_, "BtnBranchName", is_valid);
        MainCommon.show_child(ui_utility.uilib(),
             NURailBranch.ctrl_id_, "BtnBranchError", not is_valid);
    end;

    set_state = function(state)
        NURailBranch.log(string.format("[NURailBranch][%d]set_state %d", NURailBranch.game_id_, state));
        NURailBranch.state_ = state;
    end;

    set_branch_name = function(name, is_deleted)
        MainCommon.set_button_caption(ui_utility.uilib(), NURailBranch.ctrl_id_, "BtnBranchName", name);
        if is_deleted then
            MainCommon.SetButtonTextColor(ui_utility.uilib(),
                NURailBranch.ctrl_id_, "BtnBranchName", "RGB:222,22,22");
        else
            MainCommon.SetButtonTextColor(ui_utility.uilib(),
                NURailBranch.ctrl_id_, "BtnBranchName", "RGB:30,30,30");
        end;
    end;

    set_expanded = function(expanded)
        NURailBranch.log(string.format("[NURailBranch][%d]set_expanded", NURailBranch.game_id_));
        NURailBranch.expanded_ = expanded;
        
        --NURailBranch.set_expanded
        if expanded then
            ui_utility.setproperty_by_path(frame_bottom_view.bottom_branch_info_id_, 
                "CntBranchButtons\\BtnBranchUp", "Visible", "false");
            ui_utility.setproperty_by_path(frame_bottom_view.bottom_branch_info_id_, 
                "CntBranchButtons\\BtnBranchDown", "Visible", "true");  
            ui_utility.setproperty_by_path(frame_bottom_view.bottom_branch_info_id_, 
                "BtnBranchDownCover", "Visible", "true");  
        else
            ui_utility.setproperty_by_path(frame_bottom_view.bottom_branch_info_id_, 
                "CntBranchButtons\\BtnBranchDown", "Visible", "false"); 
            ui_utility.setproperty_by_path(frame_bottom_view.bottom_branch_info_id_, 
                "BtnBranchDownCover", "Visible", "false"); 
            ui_utility.setproperty_by_path(frame_bottom_view.bottom_branch_info_id_, 
                "CntBranchButtons\\BtnBranchUp", "Visible", "true"); 
        end
    end;

    get_expanded = function()
        return NURailBranch.expanded_;
    end;

    show = function(is_show)
        NURailBranch.log("show, " .. tostring(is_show) .. ", uob_branch_id:" .. tostring(NURailBranch.ctn_id_));
        ui_utility.show_ctrl(NURailBranch.ctn_id_, is_show);
        NURailBranch.is_visible_ = is_show;
        NURailBranch.set_expanded(false)
    end;

    is_visible = function()
        return NURailBranch.is_visible_;
    end;
};


------------------------------------UIEventListener-----------------------------------------------------------------

NURailBranchUIEventListener = {
};

function NURailBranchUIEventListener.on_btn_branch_down_click(UILib, dwControlID, x, y)
    NURailBranch.log(string.format("[BannerUIEventListener]on_btn_branch_click, ctrl_id:%d",
       dwControlID));

    local cnt_branch_btns_id = ui_utility.get_ctrl_parent_id(dwControlID);
    local cnt_drop_id = ui_utility.get_ctrl_parent_id(cnt_branch_btns_id);
    local uob_branch_id = ui_utility.get_ctrl_parent_id(cnt_drop_id);
    local banner_normal_id = ui_utility.get_ctrl_parent_id(uob_branch_id);  
    local banner_id = ui_utility.get_ctrl_parent_id(banner_normal_id);
    
    local banner = WGBannerList:get_banner_by_ctrl(banner_id);
    if banner == nil then
        NURailBranch.log(string.format("[NUBannerActionUIEventListener][on_btn_branch_down_click]banner is NIL," .. 
            " dwControlID:%d, got banner_id:%d",
            dwControlID, banner_id), true);
        return;
    end

    local game_id = banner.game_id_;

    local control_ptr = ui_utility.get_ctrl_ptr_by_id(banner_id);
    local rc = nil;
    if control_ptr ~= nil then
        rc = control_ptr:GetRectEx(TRUE,TRUE);
    end

    if rc ~= nil then 
        local buttun_height = rc.bottom - rc.top;
        local xoffset = rc.left;
        local yoffset = rc.bottom;
        NURailBranchList.init();
        NURailBranchList.show(xoffset, yoffset, game_id);
    end
end

function NURailBranchUIEventListener.on_btn_branch_up_click(UILib, dwControlID, x, y)
    base_utility.log(string.format("[NUBannerActionUIEventListener]on_btn_branch_click, ctrl_id:%d",
       dwControlID));

end

function NURailBranchUIEventListener.OnBranchListShow(event_id, json_str)
    base_utility.log("[BannerUIEventListener][OnBranchListShow]json_str:" .. tostring(json_str));
    local params = base_utility.json_to_table(json_str);
    if params == nil or params.game_id == nil or params.is_show == nil then
        base_utility.log("[BannerUIEventListener][OnBranchListShow]param INVALID, json_str:" ..
            tostring(json_str), true);
        return;
    end
end

event_center.add_listener(frame_bottom_event.BRANCH_LIST_SHOW, 'NURailBranchUIEventListener.OnBranchListShow');